import sys, os
sys.path.append(os.path.join(os.getcwd(),'ui/'))
from OboardCamDisp import Ui_MainWindow
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog
from PyQt5.QtCore import QTimer,QCoreApplication
from PyQt5.QtGui import QPixmap
import cv2
import qimage2ndarray
import time
sys.path.append(os.path.join(os.getcwd(),'facenet/'))
import facerec_from_webcam as fn
sys.path.append(os.path.join(os.getcwd(),'tensorflow-yolov3/'))
import tf_yolov3 as ty
from PIL import Image, ImageDraw, ImageFont

class CamShow(QMainWindow,Ui_MainWindow):
    def __del__(self):
        try:
            self.camera.release()  # 释放资源
        except:
            return
    def __init__(self,parent=None):
        super(CamShow,self).__init__(parent)
        self.setupUi(self)
        self.PrepWidgets()
        self.PrepParameters()
        self.Prepffmpeg()
        self.CallBackFunctions()
        self.Timer=QTimer()
        self.Timer.timeout.connect(self.TimerOutFun)
    def PrepWidgets(self):
        self.PrepCamera()
        self.StopBt.setEnabled(False)
        self.RecordBt.setEnabled(False)
    def PrepCamera(self):
        try:
            #self.camera=cv2.VideoCapture(0)
            self.MsgTE.clear()
            self.MsgTE.append('Oboard camera connected.')
            self.MsgTE.setPlainText()
        except Exception as e:
            self.MsgTE.clear()
            self.MsgTE.append(str(e))
    def SetNet(self):
        if self.facenet_checkBox.isChecked():
            fn.face_recognition_init()
        if self.tf_yolov3_checkBox.isChecked():
            ty.tf_yolov3_init()

    def PrepParameters(self):
        self.RecordFlag=0
        self.RecordPath_save='/home/xs/AI_dome/'
        self.FilePathLE_save.setText(self.RecordPath_save)
        self.RecordPath_load=''
        self.Image_num=0
        self.R=1
        self.G=1
        self.B=1    

        self.MsgTE.clear()
    def Prepffmpeg(self):
        self.RecordPath_ffmpeg='rtmp://127.0.0.1:1935/live/STREAM_NAME'
        self.FilePathLE_ffmpeg.setText(self.RecordPath_ffmpeg)
    def CallBackFunctions(self):
        self.FilePathBt_save.clicked.connect(self.SetFilePath_save)
        self.FilePathBt_load.clicked.connect(self.SetFilePath_load)
        self.ShowBt.clicked.connect(self.StartCamera)
        self.StopBt.clicked.connect(self.StopCamera)
        self.RecordBt.clicked.connect(self.RecordCamera)
        self.ExitBt.clicked.connect(self.ExitApp)


    def StartCamera(self):
        self.ShowBt.setEnabled(False)
        self.StopBt.setEnabled(True)
        self.RecordBt.setEnabled(True)
        self.RecordBt.setText('录像')
        self.SetNet()
        self.font = ImageFont.truetype('NotoSansCJK-Thin.ttc', 30)
        self.Timer.start(1)
        self.timelb=time.clock()
        if self.radioButton_file.isChecked():
            self.camera=cv2.VideoCapture(self.RecordPath_load)
        elif self.radioButton_ffmpeg.isChecked():
            self.camera=cv2.VideoCapture(self.RecordPath_ffmpeg)
        else:
            self.camera=cv2.VideoCapture(0)
    def SetFilePath_save(self):
        dirname_save = QFileDialog.getExistingDirectory(self, "浏览", '.')
        if dirname_save:
            self.FilePathLE_save.setText(dirname_save)
            self.RecordPath_save=dirname_save+'/'
    def SetFilePath_load(self):
        dirname_load, filetype = QFileDialog.getOpenFileName(self, "浏览",'.','图片(*.jpg);;视频(*.avi)')
        if dirname_load:
            self.FilePathLE_load.setText(dirname_load)
            self.RecordPath_load=dirname_load   
    def TimerOutFun(self):
        success,img=self.camera.read()
        if success:
            self.Image = self.ColorAdjust(img)
            self.DispImg()
            self.Image_num+=1
            if self.RecordFlag:
                self.video_writer.write(img)
            if self.Image_num%10==9:
                frame_rate=10/(time.clock()-self.timelb)
                self.FmRateLCD.display(frame_rate)
                self.timelb=time.clock()
                self.ImgWidthLCD.display(self.camera.get(3))
                self.ImgHeightLCD.display(self.camera.get(4))
        else:
            self.MsgTE.clear()
            self.MsgTE.setPlainText('Image obtaining failed.')
    def ColorAdjust(self,img):
        try:
            B=img[:,:,0]
            G=img[:,:,1]
            R=img[:,:,2]
            B=B*self.B
            G=G*self.G
            R=R*self.R

            img1=img
            img1[:,:,0]=B
            img1[:,:,1]=G
            img1[:,:,2]=R
            return img1
        except Exception as e:
            self.MsgTE.setPlainText(str(e))
    def DispImg(self):
        img = cv2.cvtColor(self.Image, cv2.COLOR_BGR2RGB)
        if self.tf_yolov3_checkBox.isChecked():
            score=0.5
            img=ty.tf_yolov3_frame(self.font,img,score)        
        if self.facenet_checkBox.isChecked():
            score=0.5
            img=fn.face_recognition_frame(self.font,img,score)
        if self.carnet_checkBox.isChecked():
            img=img
        qimg = qimage2ndarray.array2qimage(img)
        self.DispLb.setPixmap(QPixmap(qimg))
        self.DispLb.show()
    def StopCamera(self):
        if self.StopBt.text()=='暂停':
            self.StopBt.setText('继续')
            self.RecordBt.setText('保存')
            self.Timer.stop()
        elif self.StopBt.text()=='继续':
            self.StopBt.setText('暂停')
            self.RecordBt.setText('录像')
            self.Timer.start(1)
    def RecordCamera(self):
        tag=self.RecordBt.text()
        if tag=='保存':
            try:
                image_name=self.RecordPath_save+'image'+time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))+'.jpg'
                print(image_name)
                cv2.imwrite(image_name, self.Image)
                self.MsgTE.clear()
                self.MsgTE.setPlainText('Image saved.')
            except Exception as e:
                self.MsgTE.clear()
                self.MsgTE.setPlainText(str(e))
        elif tag=='录像':
            self.RecordBt.setText('停止')

            video_name = self.RecordPath_save + 'video' + time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) + '.avi'
            fps = self.FmRateLCD.value()
            size = (self.Image.shape[1],self.Image.shape[0])
            fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
            self.video_writer = cv2.VideoWriter(video_name, fourcc,self.camera.get(5), size)
            self.RecordFlag=1
            self.MsgTE.setPlainText('Video recording...')
            self.StopBt.setEnabled(False)
            self.ExitBt.setEnabled(False)
        elif tag == '停止':
            self.RecordBt.setText('录像')
            self.video_writer.release()
            self.RecordFlag = 0
            self.MsgTE.setPlainText('Video saved.')
            self.StopBt.setEnabled(True)
            self.ExitBt.setEnabled(True)
    def ExitApp(self):
        if self.radioButton_file.isChecked()|self.radioButton_ffmpeg.isChecked()|self.radioButton_camera.isChecked():
            self.camera.release()
        self.MsgTE.setPlainText('Exiting the application..')
        QCoreApplication.quit()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui=CamShow()
    ui.show()
    sys.exit(app.exec_())
